import java.util.*;

public class Test1114 {
    public static int thirdMax(int[] nums){
        TreeSet<Integer> uniqueSet = new TreeSet<>(Collections.reverseOrder());
        for (int num : nums){
            uniqueSet.add(num);
        }
        List<Integer> sortedList = new ArrayList<>(uniqueSet);

        return sortedList.size() >= 3?sortedList.get(2):sortedList.get(0);
    }
}


